package DataStructureAndAlgorithm.Acwing.DP.背包问题DP.零一背包DP.至多背包求解;

//未优化代码
import java.util.Scanner;

class dp_1024{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int v = in.nextInt();
        int n = in.nextInt();
        int temp;
        int[][] dp = new int[n + 1][v + 1];
        for (int i = 1; i <= n; i++){
            temp = in.nextInt();
            for (int j = 1; j <= v; j++){
                //不拿第i个物品的情况
                dp[i][j] = dp[i - 1][j];
                if (j >= temp){
                    dp[i][j] = Math.max(dp[i][j],dp[i - 1][j - temp] + temp);
                }
            }
        }
        //拿到最大的，则剩下的就是最小的
        System.out.print(v - dp[n][v]);
    }
}

/*
优化后代码
        import java.util.Scanner;

class dp_1024{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int v = in.nextInt();
        int n = in.nextInt();
        int temp;
        int[] dp = new int[v + 1];
        for (int i = 1; i <= n; i++){
            temp = in.nextInt();
            for (int j = v; j >= temp; j--){
                dp[j] = Math.max(dp[j],dp[j - temp] + temp);
            }
        }
        //拿到最大的，则剩下的就是最小的
        System.out.print(v - dp[v]);
    }
}
*/